package com.example.deertrip;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.example.deertrip.R;
import com.example.deertrip.bean.Flight;
import com.example.deertrip.bean.Moment;
import com.example.deertrip.database.DBOperate;
import com.example.deertrip.databinding.FragmentHomeBinding;
import com.example.deertrip.tools_class.DrawableHelp;
import com.youth.banner.Banner;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Flight_Model_Activity extends AppCompatActivity {

    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private ListView listView;
    private SimpleAdapter adapter = null;
    List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
    private DBOperate dbOperate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flight_model);
        listView = findViewById(R.id.flight_lv);
        dbOperate = new DBOperate(Flight_Model_Activity.this);
        sql();
    }


    private void sql() {

        List<Flight> flights = dbOperate.get_All_Flight_Data();

        Drawable dw;

        for (int i = 0; i < flights.size(); i++) {
            HashMap<String, Object> items = new HashMap<String, Object>(); //存放一条数据的哈希表
            Flight flight = flights.get(i);
            BitmapDrawable bd = new BitmapDrawable(this.getResources(), flight.getCompanyImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
            dw = bd;    //再转换为Drawable
            items.put("CompanyImage", dw);   //属性分别存入哈希表
            items.put("FlightNumber", flight.getFlightNumber());
            items.put("TakeoffTime", flight.getTakeoffTime());
            items.put("LandingTime", flight.getLandingTime());
            items.put("FlightForm", flight.getFlightForm());
            items.put("FlightTo", flight.getFlightTo());
            items.put("TicketPrice", flight.getTicketPrice());
            items.put("TicketPrice", flight.getTicketPrice());
            items.put("TotalHour", flight.getTotalHour());
            items.put("CompanyName", flight.getCompanyName());
            data.add(items);   //总数据哈希表中加入该条数据项
        }


     adapter = new SimpleAdapter(Flight_Model_Activity.this, data, R.layout.flight_info_bar,
                new String[]{"CompanyImage", "FlightNumber", "TakeoffTime", "LandingTime", "FlightForm", "FlightTo"}, new int[]{R.id.iv_1, R.id.tv_5, R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4});


        adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            public boolean setViewValue(View view, Object data, String textRepresentation) {
                if (view instanceof ImageView && data instanceof Drawable) {
                    ImageView iv = (ImageView) view;
                    iv.setImageDrawable((Drawable) data);
                    return true;
                } else return false;
            }
        });

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, Object> items=new HashMap<String, Object>();
                Bundle bundle=new Bundle();
                items=data.get(i);
                bundle.putString("TakeoffTime",(String)items.get("TakeoffTime"));
                bundle.putString("CompanyName",(String)items.get("CompanyName"));
                bundle.putString("TotalHour",(String)items.get("TotalHour"));
                bundle.putInt("TicketPrice",(Integer) items.get("TicketPrice"));
                System.out.println(items.get("TicketPrice"));
                bundle.putString("LandingTime",(String)items.get("LandingTime"));
                bundle.putString("FlightForm",(String)items.get("FlightForm"));
                bundle.putString("FlightTo",(String)items.get("FlightTo"));
                Intent intent=new Intent(Flight_Model_Activity.this, Flight_Choose_Activity.class);
                intent.putExtras(bundle);

                startActivity(intent);
            }
        });

    }

}